home *** CD-ROM | disk | FTP | other *** search
/ Inside Mac Games Volume 5 #3 / IMG 46 Vol 5-3.iso / More Goodies / More For Your Game / Realmz / Character Master Source / Character Master / my files.cpp < prev    next >
C/C++ Source or Header  |  1996-07-16  |  12KB  |  506 lines

  1. #include "my files.h"
  2. #include "main.h"
  3. #include "character class.h"
  4. #include "my dialogs.h"
  5. #include "MoreFilesExtras.h"
  6.  
  7. extern    nemesisGlobalPtr    G;
  8.  
  9. Boolean    IsFileRealmz( FSSpec & theSpec )
  10. {
  11.     OSErr    error    = noErr;
  12.     FInfo    theFInfo;
  13.     
  14.     error = FSpGetFInfo( &theSpec, &theFInfo );
  15.     if( error )
  16.         return false;
  17.  
  18.     if( theFInfo.fdType != kApplicationType )
  19.         error = true;
  20.  
  21.     if( theFInfo.fdCreator != kRealmzCreator )
  22.         error = true;
  23.     
  24.     if( error )    
  25.         return false;
  26.     else
  27.         return true;
  28. }
  29.  
  30. Boolean    IsOpenableFile( FSSpec & theSpec )
  31. {
  32.     OSErr    error    = noErr;
  33.     Boolean    rightType = false;
  34.     FInfo    theFInfo;
  35.     
  36.     error = FSpGetFInfo( &theSpec, &theFInfo );
  37.     if( error )
  38.         return false;
  39.  
  40.     switch( theFInfo.fdType )
  41.     {
  42.         case kFighterType:
  43.         case kMonkType:
  44.         case kPaladinType:
  45.         case kRangerType:
  46.         case kThiefType:
  47.         case kMagicType:
  48.         case kClericType:
  49.         case kEnchanterType: rightType = true;
  50.                              break;
  51.         default: rightType = false;
  52.                  break;
  53.     }
  54.         
  55.     if( error || (!rightType) )    
  56.         return false;
  57.     else
  58.         return true;
  59. }
  60.  
  61. Boolean    CheckAlreadyOpen( const FSSpec &theFile )
  62. {
  63.     Boolean            open = false;
  64.     DialogRef        theDialog = G->DialogList();
  65.     baseModelessDialogPtr    myDialog;    
  66.  
  67.     while( theDialog && !open )
  68.     {
  69.         myDialog = NemesisGetBaseMlessPtr( theDialog );
  70.         open = NemesisSameFileSpec( theFile, ((character *)(myDialog->DialogData()))->TheFileSpec() );
  71.         
  72.         if (open)
  73.         {
  74.             SelectWindow( (WindowRef)theDialog );
  75.         }
  76.  
  77.         theDialog = myDialog->NextDialog();
  78.     }
  79.     return(open);
  80. }
  81.  
  82. OSErr    DoOpen( FSSpec theSpec )
  83. {
  84.     DialogRef    theDialog = nil;
  85.     OSErr        error = noErr;
  86.     int            count;
  87.  
  88.     if( !CheckAlreadyOpen( theSpec ) )    // Check its not already open
  89.     {
  90.         // if its good, get on with setting up the dialog.
  91.         error = NemesisGetDialog( kEditCharacterDialogID, nil, kInFront, theDialog );
  92.         if( error ) return error;
  93.  
  94.         for( count = 0; count < kNumOutlines; count++ )
  95.             NemesisSetupOutline( theDialog, kECOutline[count] );
  96.  
  97.         // Stuff a pointer to the editCharacterDialog in the dialog
  98.         SetWRefCon( (WindowRef)theDialog,
  99.                     (long)(new editCharacterDialog(theDialog, nil)) );
  100.         
  101.         // Now setup the dialog with the character data
  102.         // and tell the character which dialog it belongs to.
  103.         (NemesisGetBaseMlessPtr( theDialog ))->SetDialogData( ((long)(new character(theSpec, theDialog))) );
  104.  
  105.         // Right. Now tell the character to open itself.
  106.         error = ((character *)(NemesisGetBaseMlessPtr( theDialog ))->DialogData())->OpenCharacter();
  107.         if( error ) return error;
  108.  
  109.         // Okay-dokey, now set up the dialog with the data contained in the character
  110.         ((character *)(NemesisGetBaseMlessPtr( theDialog ))->DialogData())->SetupENDialogControls();
  111.  
  112.         // Finally, set the dialog kind and show it.
  113.         NemesisSetDialogKind( theDialog, kCharacterDKind );
  114.         NemesisShowDialog( theDialog );
  115.         NemesisAddDialogToList( theDialog );
  116.     }
  117.     
  118.     return error;
  119. }
  120.  
  121. OSErr    DoOpen()
  122. {
  123.     StandardFileReply    theReply;
  124.     OSErr        error = noErr;
  125.     Point        tempPoint;
  126.     
  127.     if( !CMData()->KnowWhereRealmzIs() )
  128.         DoLocateRealmz();
  129.     
  130.     if( !CMData()->KnowWhereRealmzIs() )
  131.         return error;
  132.         
  133.     SetPt( &tempPoint, -1, -1 );
  134.  
  135.     CustomGetFile( CMData()->RealmzCharFileFilter(),
  136.                    kNumberOfTypes,
  137.                    theTypeList,
  138.                    &theReply,
  139.                    kGetCharDialogID,
  140.                    tempPoint,
  141.                    CMData()->RealmzCharDialogHook(),
  142.                    CMData()->GetFileModalFilter(),
  143.                    nil,
  144.                    nil,
  145.                    &theReply );
  146.  
  147.     if( theReply.sfGood )
  148.         error = DoOpen( theReply.sfFile );
  149.     
  150.     return error;
  151. }
  152.  
  153. void    DoLocateRealmz()
  154. {
  155.     StandardFileReply    theReply;
  156.     OSErr        error = noErr;
  157.     Point        tempPoint;
  158.     
  159.     SetPt( &tempPoint, -1, -1 );
  160.  
  161.     CustomGetFile( CMData()->RealmzAppFileFilter(),
  162.                    1,
  163.                    realmzAppType,
  164.                    &theReply,
  165.                    kGetAppDialogID,
  166.                    tempPoint,
  167.                    CMData()->RealmzAppDialogHook(),
  168.                    CMData()->GetFileModalFilter(),
  169.                    nil,
  170.                    nil,
  171.                    nil );
  172.  
  173.     if( theReply.sfGood )
  174.     {
  175.         CMData()->SetRealmzLocation( theReply.sfFile );
  176.         CMData()->SetKnowWhereRealmzIs( true );
  177.         DoReadRealmzData();
  178.     }
  179.     else
  180.         CMData()->SetKnowWhereRealmzIs( false );
  181. }
  182.  
  183. void    DoReadRealmzData()
  184. {
  185.     int        refNum = 0;
  186.     short    tempHit;
  187.     int        count1, count2;
  188.     OSErr    error    = noErr;
  189.     Str255    tempStr;
  190.     Str255    tempDesc;
  191.     Str255    whatImDoing = "\p";
  192.     DialogRef    theDialog;
  193.     
  194.     SetCursor( &G->WatchCursor() );
  195.     error = NemesisGetDialog( kLoadingDataID, nil, kInFront, theDialog );
  196.     if( error )
  197.     {
  198.         SysBeep( 30 );
  199.         theDialog = nil;
  200.     }
  201.             
  202.     if( theDialog )
  203.     {
  204.         SelectWindow( theDialog );
  205.         ShowWindow( theDialog );
  206.         DrawDialog( theDialog );
  207.         NemesisGetIndString( kDoingStringID, kOpeningRealmz, whatImDoing );
  208.         NemesisSetItemText( theDialog, kLoadingDataText, whatImDoing );
  209.     }
  210.         
  211.     refNum = FSpOpenResFile( &(CMData()->RealmzLocation()), fsRdPerm );
  212.     if( refNum < 0 )
  213.     {
  214.         if( theDialog )
  215.         {
  216.             NemesisGetIndString( kDoingStringID, kClosingRealmzError, whatImDoing );
  217.             NemesisSetItemText( theDialog, kLoadingDataText, whatImDoing );
  218.         }
  219.  
  220.         CloseResFile( refNum );    // Shouldn't be necessary but just in case
  221.         UseResFile( G->DefaultResFile() ); // Just in case
  222.  
  223.         if( theDialog )
  224.             DisposeDialog( theDialog );
  225.  
  226.         SetCursor( &G->ArrowCursor() );
  227.         CMData()->SetKnowWhereRealmzIs( false );
  228.         NemesisShowError( kResFromRealmzError );
  229.         return;
  230.     }
  231.     
  232.     UseResFile( refNum );
  233.     
  234.     if( theDialog )
  235.     {
  236.         NemesisGetIndString( kDoingStringID, kReadingSpellNames, whatImDoing );
  237.         NemesisSetItemText( theDialog, kLoadingDataText, whatImDoing );
  238.     }
  239.         
  240.     for( count1 = 0; count1 < 7; count1++ )
  241.     {
  242.         for( count2 = 0; count2 < 12; count2++ )
  243.         {
  244.             NemesisPStrCpy( tempStr, "\p«Unknown»" );    // Just in case this fails
  245.             GetIndString( tempStr, kMagicUserSpells[count1], (count2+1) );
  246.             CMData()->SetSpellName( kMagicSpellList, count1, count2, tempStr );
  247.         }
  248.     }
  249.  
  250.     for( count1 = 0; count1 < 7; count1++ )
  251.     {
  252.         for( count2 = 0; count2 < 12; count2++ )
  253.         {
  254.             NemesisPStrCpy( tempStr, "\p«Unknown»" );    // Just in case this fails
  255.             GetIndString( tempStr, kClericSpells[count1], (count2+1) );
  256.             CMData()->SetSpellName( kClericSpellList, count1, count2, tempStr );
  257.         }
  258.     }
  259.  
  260.     for( count1 = 0; count1 < 7; count1++ )
  261.     {
  262.         for( count2 = 0; count2 < 12; count2++ )
  263.         {
  264.             NemesisPStrCpy( tempStr, "\p«Unknown»" );    // Just in case this fails
  265.             GetIndString( tempStr, kEnchanterSpells[count1], (count2+1) );
  266.             CMData()->SetSpellName( kEnchanterSpellList, count1, count2, tempStr );
  267.         }
  268.     }
  269.  
  270.     if( theDialog )
  271.     {
  272.         NemesisGetIndString( kDoingStringID, kReadingSpellDescriptions, whatImDoing );
  273.         NemesisSetItemText( theDialog, kLoadingDataText, whatImDoing );
  274.     }
  275.  
  276.     for( count1 = 0; count1 < 7; count1++ )
  277.     {
  278.         for( count2 = 0; count2 < 12; count2++ )
  279.         {
  280.             NemesisPStrCpy( tempDesc, "\p«Unknown»" );    // Just in case this fails
  281.             GetIndString( tempDesc, (kMagicUserSpells[count1] * -1), (count2+1) );
  282.             CMData()->SetSpellDescription( kMagicSpellList, count1, count2, tempDesc );
  283.         }
  284.     }
  285.  
  286.     for( count1 = 0; count1 < 7; count1++ )
  287.     {
  288.         for( count2 = 0; count2 < 12; count2++ )
  289.         {
  290.             NemesisPStrCpy( tempDesc, "\p«Unknown»" );    // Just in case this fails
  291.             GetIndString( tempDesc, (kClericSpells[count1] * -1), (count2+1) );
  292.             CMData()->SetSpellDescription( kClericSpellList, count1, count2, tempDesc );
  293.         }
  294.     }
  295.  
  296.     for( count1 = 0; count1 < 7; count1++ )
  297.     {
  298.         for( count2 = 0; count2 < 12; count2++ )
  299.         {
  300.             NemesisPStrCpy( tempDesc, "\p«Unknown»" );    // Just in case this fails
  301.             GetIndString( tempDesc, (kEnchanterSpells[count1] * -1), (count2+1) );
  302.             CMData()->SetSpellDescription( kEnchanterSpellList, count1, count2, tempDesc );
  303.         }
  304.     }
  305.  
  306.     if( theDialog )
  307.     {
  308.         NemesisGetIndString( kDoingStringID, kClosingRealmz, whatImDoing );
  309.         NemesisSetItemText( theDialog, kLoadingDataText, whatImDoing );
  310.     }
  311.     CloseResFile( refNum );
  312.     UseResFile( G->DefaultResFile() );
  313.  
  314.     if( theDialog )
  315.     {
  316.         NemesisGetIndString( kDoingStringID, kSettingUpDirectories, whatImDoing );
  317.         NemesisSetItemText( theDialog, kLoadingDataText, whatImDoing );
  318.     }
  319.     
  320.     // Setup char ƒ
  321.     NemesisGetIndString( kFilenamesID, kCharacterF, whatImDoing );
  322.     OSErr    newError    = noErr;
  323.     FSSpec    tempSpec;
  324.     long    tempDirID;
  325.     Boolean    isDir;
  326.     tempSpec = CMData()->RealmzLocation();
  327.     NemesisPStrCpy( tempSpec.name, whatImDoing );
  328.     newError = FSpGetDirectoryID( &tempSpec, &tempDirID, &isDir );
  329.     
  330.     if( isDir && (!newError) )
  331.     {
  332.         tempSpec.parID = tempDirID;
  333.         tempSpec.name[0] = 0;
  334.     }
  335.         
  336.     CMData()->SetCharFLocation( tempSpec );
  337.     
  338.     // Setup DataCD location
  339.     if( isDir && (!newError) )
  340.     {
  341.         NemesisGetIndString( kFilenamesID, kDataCD, whatImDoing );
  342.         NemesisPStrCpy( tempSpec.name, whatImDoing );
  343.         CMData()->SetIsDataCD( true );
  344.     }
  345.     else
  346.         CMData()->SetIsDataCD( false );
  347.     
  348.     CMData()->SetCharDataCDLocation( tempSpec );
  349.  
  350.     // Setup save ƒ location
  351.     NemesisGetIndString( kFilenamesID, kSaveF, whatImDoing );
  352.     tempSpec = CMData()->RealmzLocation();
  353.     NemesisPStrCpy( tempSpec.name, whatImDoing );
  354.     newError = FSpGetDirectoryID( &tempSpec, &tempDirID, &isDir );
  355.     
  356.     if( isDir && (!newError) )
  357.     {
  358.         tempSpec.parID = tempDirID;
  359.         tempSpec.name[0] = 0;
  360.     }
  361.         
  362.     CMData()->SetSaveFLocation( tempSpec );
  363.         
  364.     if( theDialog )
  365.         DisposeDialog( theDialog );
  366.         
  367.     SetCursor( &G->ArrowCursor() );
  368. }
  369.  
  370. pascal    Boolean    MyRealmzAppFileFilter( CInfoPBPtr pb, void * myData )
  371. {
  372.     Boolean    dontShowFile    = true;
  373.     
  374.     if( pb->hFileInfo.ioFlFndrInfo.fdCreator == kRealmzCreator )
  375.         dontShowFile = false;
  376.     else
  377.         dontShowFile = true;
  378.     
  379.     return dontShowFile;
  380. }
  381.  
  382. pascal    Boolean    MyRealmzCharFileFilter( CInfoPBPtr pb, void * myData )
  383. {
  384.     Boolean    dontShowFile    = true;
  385.  
  386.     if( CMData()->ShowAll() )
  387.     {
  388.         if( pb->hFileInfo.ioFlFndrInfo.fdCreator == kRealmzCreator )
  389.             dontShowFile = false;
  390.         else
  391.             dontShowFile = true;
  392.     }
  393.     else
  394.     {
  395.         if( (pb->hFileInfo.ioFlFndrInfo.fdType == CMData()->TypeToShow() ) &&
  396.             (pb->hFileInfo.ioFlFndrInfo.fdCreator == kRealmzCreator) )
  397.             dontShowFile = false;
  398.         else
  399.             dontShowFile = true;
  400.     }
  401.  
  402.     return dontShowFile;
  403. }
  404.  
  405. pascal    int    MyRealmzCharDialogHook( int itemHit, DialogRef theDialog, void * myData )
  406. {
  407.     int    itemHitToReturn    = itemHit;
  408.     int    temp = 0;
  409.     
  410.     switch( itemHit )
  411.     {
  412.         // Check for first call
  413.         case sfHookFirstCall :    NemesisSetControlValue( theDialog, kClassPopup, CMData()->CurrentType() );
  414.                                 itemHitToReturn = sfHookNullEvent;
  415.                                 if( CMData()->FirstTimeOpenedCharacter() )
  416.                                 {
  417.                                     CMData()->SetFirstTimeOpenedCharacter( false );
  418.                                     ((StandardFileReply*)(myData))->sfFile.parID = CMData()->CharFLocation().parID;
  419.                                     ((StandardFileReply*)(myData))->sfFile.vRefNum = CMData()->CharFLocation().vRefNum;
  420.                                     itemHitToReturn = sfHookChangeSelection;
  421.                                 }
  422.                                 break;
  423.         case sfItemOpenButton :
  424.         case sfItemCancelButton :
  425.         case sfItemVolumeUser :
  426.         case sfItemEjectButton :
  427.         case sfItemDesktopButton : NemesisPlayGoodClick();
  428.                                    break;
  429.         case kClassPopup : temp = NemesisGetControlValue( theDialog, kClassPopup );
  430.                            if( temp != CMData()->CurrentType() )
  431.                            {
  432.                                    CMData()->SetCurrentType( temp );
  433.                                    itemHitToReturn = sfHookRebuildList;
  434.                                }
  435.                                break;
  436.         default : itemHitToReturn    = itemHit;
  437.                   break;
  438.     }
  439.     
  440.     return itemHitToReturn;
  441. }
  442.  
  443. pascal    int    MyRealmzAppDialogHook( int itemHit, DialogRef theDialog, void * myData )
  444. {
  445.     int    itemHitToReturn    = itemHit;
  446.     int    temp = 0;
  447.     
  448.     switch( itemHit )
  449.     {
  450.         case sfItemOpenButton :
  451.         case sfItemCancelButton :
  452.         case sfItemVolumeUser :
  453.         case sfItemEjectButton :
  454.         case sfItemDesktopButton : NemesisPlayGoodClick();
  455.                                    break;
  456.         default : itemHitToReturn    = itemHit;
  457.                   break;
  458.     }
  459.     
  460.     return itemHitToReturn;
  461. }
  462.  
  463. pascal    Boolean    MyGetFileModalFilter( DialogRef theDialog, EventRecord * theEvent, int * itemHit, void * myData )
  464. {
  465.     Boolean    haveHandled    = false;
  466.     Rect    dragRect;
  467.     WindowRef    hitWindow    = nil;
  468.     short    windowPart = FindWindow(theEvent->where, &hitWindow);
  469.     
  470.     switch( theEvent->what )
  471.     {
  472.         case updateEvt :
  473.             if( ((DialogRef)theEvent->message) != ((DialogRef)FrontWindow()) )
  474.             {
  475.                 baseDialogPtr    myDialog = NemesisGetBaseDPtr( (DialogRef)theEvent->message );
  476.                 if( myDialog )
  477.                 {
  478.                     myDialog->DoUpdate( nil );
  479.                     haveHandled = true;
  480.                 }
  481.             }
  482.             break;
  483.         case mouseDown:
  484.             if ( theEvent->modifiers & cmdKey )
  485.             {
  486.  
  487.                 if (windowPart == inDrag)
  488.                 {
  489.                     dragRect = (**GetGrayRgn()).rgnBBox;
  490.                     DragWindow( hitWindow, theEvent->where, &dragRect );
  491.                     haveHandled = true;
  492.                 }
  493.             }
  494.             else
  495.             {
  496.                 if( windowPart == inDrag && (hitWindow == theDialog))
  497.                 {
  498.                     dragRect = (**GetGrayRgn()).rgnBBox;
  499.                     DragWindow( hitWindow, theEvent->where, &dragRect );
  500.                     haveHandled = true;
  501.                 }
  502.             }
  503.     }
  504.     
  505.     return haveHandled;
  506. }